<?
require("inc/common.php");
require("bin/db.php");
$VALIDATE_USER="no";
require("bin/fc.php");

$action = $_POST["action"];
$username = addslashes($_POST["username"]);
$password = addslashes($_POST["password"]);

if (isset($HTTP_POST_VARS["language"])) {
	if ($HTTP_POST_VARS["language"] == "1") {
		setcookie("language", "JP");
		define("LANGUAGE", "JP");
	} else {
		setcookie("language", "EN");
	}
}

$status = "Please enter your username and password to login. ";

if (isset($action) && $action=="login") {
	$postValidCode = $_POST["validCode"];
	$sessionValidCode = $_SESSION["loginauthcode"];
	if($postValidCode != $sessionValidCode)
	{
		$status = "<font color=red>Invalid validcode! Please try again.</font>";
	}
    else
	{
		if (!validate($username,$password)){
			$status = "<font color=red>Invalid username or password! Please try again.</font>";
		} else {
			$table = "user_profile";
			$Query = "SELECT name, email, type,currlogin FROM $table WHERE username='$username' AND BINARY password='$password'";
			$r=myQUERY($Query);
		
			$name = $r[0][0];
			$type = $r[0][2];
			$_SESSION['lastlogin']=$r[0][3];
	
			session_start(); 
			session_register("admin_username");
			session_register("admin_name");
			session_register("admin_type");
			if (defined("LANGUAGE") && LANGUAGE=="JP") 
				$_SESSION['lang']='JP';
			else $_SESSION['lang']='EN';
		
			$admin_username = $username;
			$admin_name = $name;
			$admin_type = $type;
	
			$t = time();
			$Query = "update $table set lastlogin=currlogin,currlogin=$t where username='$username' AND BINARY password='$password'";
			$u=myQuery($Query);
	
			if (!$r) $admin_type = 9;
	
			header("Location: main.php");
			exit();
		}
	}	
}
?>

<html>
<head>
<title>i.Trade</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<link rel="stylesheet" href="inc/css.css" type="text/css">

</head>

<body bgcolor="8DAAE6" text="#000000">
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<form name="form1" method="post" action="<?echo $PHP_SELF?>">
<input name="action" type="hidden" value="login">
  <table width="440" border="0" align="center">
    <tr> 
      <td><span class="Heading">i.Trade</span></td>
    </tr>
    <tr> 
      <td> <p><?print($status)?></p>
        <table border="0" align="right">
          <tr> 
            <td>
              <div align="right">username</div>            </td>
            <td> 
              <input type="text" name="username" class="text">            </td>
          </tr>
          <tr> 
            <td>
              <div align="right">password</div>            </td>
            <td> 
              <input type="password" name="password" class="text">            </td>
          </tr>
          <tr>
            <td>validcode</td>
            <td><input name="validCode" type="text" size="10" class="text">
              <img src="authimg.php"></td>
          </tr>
          <tr> 
            <td>
              <div align="right">&nbsp;</div>            </td>
            <td> 
			 <!-- <select name="language" id="language">
			  <option value="0" selected>English</option>
			  <option value="1">Japanese</option>
			  </select> -->
			  <input type="hidden" name="language" value="0">            </td>
          </tr>
          <tr> 
            <td>&nbsp;</td>
            <td> 
              <div align="right">
                <input type="submit" name="Login" value="Login" class="button">
              </div>            </td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
</form>
</body>
</html>
